#!/usr/bin/env jython

# Copyright 2010 Axial Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import suds.client

import axialproject.configuration

from suds.client import Client as SOAPClient
from axialproject.configuration import config

reload(axialproject.configuration)


class CCRHandler(object):
  # *** Private and protected members ***

  def _prepareSOAPCall(self):
    try:
      self.client
    except NameError:
      self.client = None

    if self.client is None:
      self.client = SOAPClient(config['CCR_SERVICE_WSDL'])
      self.client.service.CCRServiceLogin(config['CCR_SERVICE_USER'], config['CCR_SERVICE_PASSWD'])


  # *** Public methods ***

  def __init__(self):
    self.client = None


  def fetchAllPatients(self):
    self._prepareSOAPCall()

    return self.client.service.GetAllPatients()


  def fetchPatientByDFN(self, patientID):
    self._prepareSOAPCall()

    return self.client.service.GetCCRForPatientDFN(patientID)
    #return self.client.service.LookupPatientsByID(patientID)


# *** Driver ***

def test():
  h = CCRHandler()

  return h.fetchAllPatients()

